package com.sailfish.springbootdemo.dao.db1;

import com.sailfish.springbootdemo.common.DB1TableName;
import com.sailfish.springbootdemo.pojo.db1.PcbaDeviceRelatedDeviceParam;
import com.sailfish.springbootdemo.pojo.db1.PcbaDeviceRelatedTechParam;
import com.sailfish.springbootdemo.pojo.db1.PcbaDeviceRelatedTechParamActual;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface PcbaDeviceRelatedTechParamActualDao {

    /**
     * 根据设备id查询
     *
     * @param pcbaDeviceId
     * @return
     */
    @Results(id = "pcbaDeviceRelatedTechParamActualResultMap", value = {
            @Result(column = "pcba_device_related_tech_param_actual_id", property = "pcbaDeviceRelatedTechParamActualId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcba_device_id", property = "pcbaDeviceId", jdbcType = JdbcType.INTEGER),
            @Result(column = "tech_device_param_key", property = "techDeviceParamKey", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pcba_device_type_id", property = "pcbaDeviceTypeId", jdbcType = JdbcType.INTEGER),
            @Result(column = "tech_device_param_value", property = "techDeviceParamValue", jdbcType = JdbcType.VARCHAR),
            @Result(column = "tech_device_param_value_audit", property = "techDeviceParamValueAudit", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
    })
    @Select("select * from " + DB1TableName.PCBA_DEVICE_RELATED_TECH_PARAM_ACTUA + " where pcba_device_id = #{pcbaDeviceId} order by pcba_device_related_tech_param_actual_id")
    List<PcbaDeviceRelatedTechParamActual> getAllByDeviceIdActual(@Param("pcbaDeviceId") Integer pcbaDeviceId);

    /**
     * 根据设备id和key查询
     *
     * @param pcbaDeviceId
     * @param techDeviceParamKey
     * @return
     */
    @ResultMap(value = "pcbaDeviceRelatedTechParamActualResultMap")
    @Select("select * from " + DB1TableName.PCBA_DEVICE_RELATED_TECH_PARAM_ACTUA + "  where pcba_device_id = #{pcbaDeviceId} and tech_device_param_key = #{techDeviceParamKey}")
    PcbaDeviceRelatedTechParamActual getPcbaDeviceRelatedTechParamActualByPcbaDeviceIdAndKey(@Param("pcbaDeviceId") Integer pcbaDeviceId, @Param("techDeviceParamKey") String techDeviceParamKey);

    /**
     * 根据key查询
     *
     * @param techDeviceParamKey
     * @return
     */
    @ResultMap(value = "pcbaDeviceRelatedTechParamActualResultMap")
    @Select("select * from " + DB1TableName.PCBA_DEVICE_RELATED_TECH_PARAM_ACTUA + "  where tech_device_param_key = #{techDeviceParamKey}")
    List<PcbaDeviceRelatedTechParamActual> getPcbaDeviceRelatedTechParamActualByKey(@Param("techDeviceParamKey") String techDeviceParamKey);

    /**
     * 新增
     *
     * @param pcbaDeviceRelatedTechParamActual
     * @return
     */
    @Insert("insert into " + DB1TableName.PCBA_DEVICE_RELATED_TECH_PARAM_ACTUA + "(pcba_device_id,tech_device_param_key,pcba_device_type_id,tech_device_param_value,tech_device_param_value_audit,create_time) values " +
            "(#{pcbaDeviceRelatedTechParamActual.pcbaDeviceId},#{pcbaDeviceRelatedTechParamActual.techDeviceParamKey},#{pcbaDeviceRelatedTechParamActual.pcbaDeviceTypeId},#{pcbaDeviceRelatedTechParamActual.techDeviceParamValue},#{pcbaDeviceRelatedTechParamActual.techDeviceParamValueAudit},#{pcbaDeviceRelatedTechParamActual.createTime})")
    Integer addTechRelatedParamActual(@Param("pcbaDeviceRelatedTechParamActual") PcbaDeviceRelatedTechParamActual pcbaDeviceRelatedTechParamActual);

    /**
     * 更新
     *
     * @param pcbaDeviceRelatedTechParamActual
     * @return
     */
    @Update("<script>" +
            "update " + DB1TableName.PCBA_DEVICE_RELATED_TECH_PARAM_ACTUA +
            "<set>" +
            "<if test='pcbaDeviceRelatedTechParamActual.pcbaDeviceId != null and pcbaDeviceRelatedTechParamActual.pcbaDeviceId != \"\" '>" +
            " pcba_device_id = #{pcbaDeviceRelatedTechParamActual.pcbaDeviceId}," +
            "</if>" +
            "<if test='pcbaDeviceRelatedTechParamActual.techDeviceParamKey != null and pcbaDeviceRelatedTechParamActual.techDeviceParamKey != \"\" '>" +
            " tech_device_param_key = #{pcbaDeviceRelatedTechParamActual.techDeviceParamKey}," +
            "</if>" +
            "<if test='pcbaDeviceRelatedTechParamActual.pcbaDeviceTypeId != null and pcbaDeviceRelatedTechParamActual.pcbaDeviceTypeId != \"\" '>" +
            " pcba_device_type_id = #{pcbaDeviceRelatedTechParamActual.pcbaDeviceTypeId}," +
            "</if>" +
            "<if test='pcbaDeviceRelatedTechParamActual.techDeviceParamValue != null '>" +
            " tech_device_param_value = #{pcbaDeviceRelatedTechParamActual.techDeviceParamValue}," +
            "</if>" +
            "<if test='pcbaDeviceRelatedTechParamActual.techDeviceParamValueAudit != null '>" +
            " tech_device_param_value_audit = #{pcbaDeviceRelatedTechParamActual.techDeviceParamValueAudit}," +
            "</if>" +
            "<if test='pcbaDeviceRelatedTechParamActual.updateTime != null '>" +
            " update_time = #{pcbaDeviceRelatedTechParamActual.updateTime}," +
            "</if>" +
            "</set>" +
            " where pcba_device_related_tech_param_actual_id = #{pcbaDeviceRelatedTechParamActual.pcbaDeviceRelatedTechParamActualId}" +
            "</script>")
    Integer updateDeviceRelatedParamActual(@Param("pcbaDeviceRelatedTechParamActual") PcbaDeviceRelatedTechParamActual pcbaDeviceRelatedTechParamActual);

    /**
     * 根据pcbaDeviceId 删除
     *
     * @param pcbaDeviceId
     * @return
     */
    @Delete("delete from " + DB1TableName.PCBA_DEVICE_RELATED_TECH_PARAM_ACTUA + " where pcba_device_id = #{pcbaDeviceId}")
    Integer deleteDeviceRelatedParamActual(@Param("pcbaDeviceId") Integer pcbaDeviceId);

    /**
     * 清空对应资源的临时值（审核通过）
     */
    @Update("update " + DB1TableName.PCBA_DEVICE_RELATED_TECH_PARAM_ACTUA + " set tech_device_param_value_audit = '' where pcba_device_id = #{pcbaDeviceId}")
    Integer clearValueAudit(@Param("pcbaDeviceId") Integer pcbaDeviceId);

    /**
     * 将 value 还原成 ValueAudit 的值（审核不通过）
     */
    @Update("update " + DB1TableName.PCBA_DEVICE_RELATED_TECH_PARAM_ACTUA + " set tech_device_param_value = tech_device_param_value_audit where pcba_device_id = #{pcbaDeviceId}")
    Integer returnValueFromValueAudit(@Param("pcbaDeviceId") Integer pcbaDeviceId);

    /**
     * 记录 ValueAudit 来自 value
     */
    @Update("update " + DB1TableName.PCBA_DEVICE_RELATED_TECH_PARAM_ACTUA + " set tech_device_param_value_audit = tech_device_param_value where pcba_device_id = #{pcbaDeviceId}")
    Integer returnValueAuditFromValue(@Param("pcbaDeviceId") Integer pcbaDeviceId);
}
